package com.ivan.stu.notetool.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.ivan.stu.notetool.activity.AddNoteActivity;
import com.ivan.stu.notetool.activity.NoteActivity;
import com.ivan.stu.notetool.callback.OpenNoteCallBack;
import com.ivan.stu.notetool.model.NotifyFinishEvent;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by IVAN on 2018/10/11.
 */

public class NoteToolManager {
    private static volatile NoteToolManager noteToolManager = null;
    private SharedPreferences userSettings;

    private NoteToolManager(){}

    public static NoteToolManager getInstance(){
        synchronized (NoteToolManager.class){
            if(noteToolManager == null){
                noteToolManager = new NoteToolManager();
            }
        }
        return noteToolManager;
    }

    public void openLancooNoteTool(Context context, String url, String schoolId, String userId, String userName, int userType, String token, String systemId,String systemName,String subjectID,String subjectName,String resourceName,String resourceID, String materialID,String materialName,int materialTotal,String resourceAndroidLink,OpenNoteCallBack callBack){

        ConstrantManager.resetData();   //重置全局静态变量

        if (verifyParam(context,url,schoolId,userId,userName,userType,token,systemId,subjectID,resourceID)){

            if (callBack!=null){

                String result = "开启成功";
                callBack.onSuccess(result);
            }


            Intent intent = new Intent(context, NoteActivity.class);

            ConstrantManager.NOTE_URL = url;
            ConstrantManager.UPLOAD_URL = url+"api/V2/Notes/UploadImg";
            ConstrantManager.childSchoolID = schoolId;
            ConstrantManager.childUserID = userId;
            ConstrantManager.childUserName = userName;
            ConstrantManager.userType = userType;
            ConstrantManager.Token = token;
            ConstrantManager.currentSystemid = systemId;  //集成系统的唯一标识
            ConstrantManager.currentSystemName = systemName;
            ConstrantManager.currentResourceName  = resourceName;
            ConstrantManager.currentResourceID = resourceID;
            ConstrantManager.currentNoteSubjectID = subjectID;
            ConstrantManager.currnetNoteSubject = subjectName;
            ConstrantManager.MaterialID = materialID;
            ConstrantManager.MaterialName = materialName;
            ConstrantManager.MaterialTotal = materialTotal;
            ConstrantManager.ResourceAndroidLink = resourceAndroidLink;

            intent.putExtra("systemId",systemId);
            intent.putExtra("userId",userId);
            intent.putExtra("userType",userType);
            intent.putExtra("userName",userName);
            intent.putExtra("schoolId",schoolId);
            intent.putExtra("token",token);
            context.startActivity(intent);

        }else {

            String reason = "参数不合法，开启笔记工具失败！";
            TextViewUtils.showToast(context,0,reason);
            if (callBack!=null){

                callBack.onFailure(reason);

            }
        }
    }

    public void closeNotetool(){

   //     ActivityCollectorUtils.finishAll();

        NotifyFinishEvent event = new NotifyFinishEvent();
        event.setFinishSigin(1);
        EventBus.getDefault().post(event);

    }



    private boolean verifyParam(Context context, String url, String schoolId, String userId, String userName, int userType, String token, String systemId,String subjecId,String resourceId) {

        if ((url!=null&&!url.equals("")) && (schoolId!=null&&!schoolId.equals("")) && (userName!=null&&!userName.equals("")) && userType == 2 && (token!=null&&!token.equals("")) && (systemId!=null&&!systemId.equals("")) && (subjecId!=null&&!subjecId.equals(""))){

            return true;

        }else {

            return false;
        }
    }
}
